﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;
using MaxAlarm.Core.Items;
using MaxAlarm.Windows;

namespace MaxAlarm.ViewModels
{
    public class MessageBoxViewModel : DependencyObject
    {
        #region Command

        private static readonly ICommand _oKCommand = new RoutedUICommand("OK", "OKCommand", typeof(MessageBoxViewModel));

        public static ICommand OKCommand
        {
            get { return _oKCommand; }
        }

        private static readonly ICommand _cancelCommand = new RoutedUICommand("Cancel", "CancelCommand", typeof(MessageBoxViewModel));

        public static ICommand CancelCommand
        {
            get { return _cancelCommand; }
        }

        private static readonly ICommand _yesCommand = new RoutedUICommand("Yes", "YesCommand", typeof(MessageBoxViewModel));

        public static ICommand YesCommand
        {
            get { return _yesCommand; }
        }

        private static readonly ICommand _noCommand = new RoutedUICommand("No", "NoCommand", typeof(MessageBoxViewModel));

        public static ICommand NoCommand
        {
            get { return _noCommand; }
        }

        private static void NoCommandExecute(object sender, ExecutedRoutedEventArgs e)
        {
            SetResult(sender, DialogResult.No);
        }

        private static void YesCommandExecute(object sender, ExecutedRoutedEventArgs e)
        {
            SetResult(sender, DialogResult.Yes);
        }

        private static void CancelCommandExecute(object sender, ExecutedRoutedEventArgs e)
        {
            SetResult(sender, DialogResult.Cancel);
        }

        private static void OKCommandExecute(object sender, ExecutedRoutedEventArgs e)
        {
            SetResult(sender, DialogResult.OK);
        }

        private static void SetResult(object sender, DialogResult result)
        {
            var window = (MaxMessageBox)sender;
            window.DialogResult = result;
            window.Close();
        }

        #endregion

        #region Properties

        public static readonly DependencyProperty MessageTitleProperty =
            DependencyProperty.Register("MessageTitle", typeof(string), typeof(MessageBoxViewModel), new PropertyMetadata(default(string)));

        public string MessageTitle
        {
            get { return (string)GetValue(MessageTitleProperty); }
            set { SetValue(MessageTitleProperty, value); }
        }

        public static readonly DependencyProperty MessageTextProperty =
            DependencyProperty.Register("MessageText", typeof(string), typeof(MessageBoxViewModel), new PropertyMetadata(default(string)));

        public string MessageText
        {
            get { return (string)GetValue(MessageTextProperty); }
            set { SetValue(MessageTextProperty, value); }
        }

        public static readonly DependencyProperty MessageLevelProperty =
            DependencyProperty.Register("MessageLevel", typeof(MessageLevel), typeof(MessageBoxViewModel), new PropertyMetadata(MessageLevel.Info));

        public MessageLevel MessageLevel
        {
            get { return (MessageLevel)GetValue(MessageLevelProperty); }
            set { SetValue(MessageLevelProperty, value); }
        }

        public static readonly DependencyProperty ButtonsProperty =
            DependencyProperty.Register("Buttons", typeof(MessageBoxButton), typeof(MessageBoxViewModel), new PropertyMetadata(MessageBoxButton.OK));

        public MessageBoxButton Buttons
        {
            get { return (MessageBoxButton)GetValue(ButtonsProperty); }
            set { SetValue(ButtonsProperty, value); }
        }

        public new DialogResult DialogResult
        {
            get { return (DialogResult)GetValue(MaxMessageBox.DialogResultProperty); }
            set { SetValue(MaxMessageBox.DialogResultProperty, value); }
        }

        #endregion

        static MessageBoxViewModel()
        {
            CommandManager.RegisterClassCommandBinding(typeof (MaxMessageBox), new CommandBinding(OKCommand, OKCommandExecute));
            CommandManager.RegisterClassCommandBinding(typeof (MaxMessageBox), new CommandBinding(CancelCommand, CancelCommandExecute));
            CommandManager.RegisterClassCommandBinding(typeof(MaxMessageBox), new CommandBinding(YesCommand, YesCommandExecute));
            CommandManager.RegisterClassCommandBinding(typeof(MaxMessageBox), new CommandBinding(NoCommand, NoCommandExecute));
        }
    }
}
